package cn.bugstack.wrench.dynamic.config.center.config;

import cn.bugstack.wrench.dynamic.config.center.service.IDynamicConfigCenterService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;

/**实现了 Spring 的 BeanPostProcessor 接口，在 Spring 容器实例化 Bean 后，自动拦截并代理带有 @DCCValue 注解的属性。
 * 通过调用 dynamicConfigCenterService.proxyObject(bean)，实现对目标 Bean 属性的动态注入和实时变更，
 * 确保属性值能够根据 Redis 配置中心的变更自动刷新。
 * @author chenjia
 */
@Configuration
public class DynamicConfigCenterAutoConfig implements BeanPostProcessor {

    private final IDynamicConfigCenterService dynamicConfigCenterService;

    public DynamicConfigCenterAutoConfig(IDynamicConfigCenterService dynamicConfigCenterService) {
        this.dynamicConfigCenterService = dynamicConfigCenterService;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return dynamicConfigCenterService.proxyObject(bean);
    }
}
